package com.resolution.dietaryassessment.view.mydietary.customview;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.example.rxbus.RxBus;
import com.example.rxbus.bean.BreakFastEvent;
import com.example.rxbus.bean.DinnerEvent;
import com.example.rxbus.bean.LunchEvent;
import com.jdqm.tapelibrary.TapeView;
import com.resolution.dietaryassessment.R;
import com.resolution.dietaryassessment.view.mydietary.bean.FoodInfoBean;
import com.resolution.dietaryassessment.view.mydietary.bean.FoodTotalCalories;

import static com.resolution.dietaryassessment.view.mydietary.constant.ConstantBean.BREAKFAST_STATUS;
import static com.resolution.dietaryassessment.view.mydietary.constant.ConstantBean.DINNER_STATUS;
import static com.resolution.dietaryassessment.view.mydietary.constant.ConstantBean.LUNCH_STATUS;

/**
 * @author By Resolusion
 * @Title:
 * @Date: 2020/4/21
 * @Time: 16:52
 * @email Res01ution@foxmail.com
 */
public class CustomDialog extends Dialog implements View.OnClickListener{

    private TextView mWeight;
    private TextView mCalories_d;
    private TextView mValue;

    private int mTotalCalories;
    private int flag = 1;

    private FoodInfoBean foodInfoBean;
    private FoodTotalCalories foodTotalCalories = new FoodTotalCalories(0,null,0,-1);
    private int state;


    @SuppressLint("SetTextI18n")
    public CustomDialog(@NonNull Context context, FoodInfoBean foodInfoBean,int state) {
        super(context,R.style.CustomDialog);
        this.foodInfoBean = foodInfoBean;
        this.state = state;
        init();
    }

    @SuppressLint("SetTextI18n")
    private void init() {
        int calories = (int) (100 * foodInfoBean.getReliang());
        setContentView(R.layout.custom_dialog);
        TapeView tapeView = findViewById(R.id.rx_view);
        mWeight = findViewById(R.id.tx_weight);
        ImageView mImageView = findViewById(R.id.close_img);
        mValue = findViewById(R.id.value_weight);
        mCalories_d = findViewById(R.id.calories);
        Button mButton = findViewById(R.id.sure_btn);
        TextView mNameTx = findViewById(R.id.tx_food_name);
        TextView mCalories = findViewById(R.id.tx_re_liang);

        mImageView.setOnClickListener(this);
        mButton.setOnClickListener(this);

        mWeight.setText(tapeView.getValue() + " " + "克");
        mValue.setText(tapeView.getValue() + " " + "克");
        mCalories_d.setText((int)(tapeView.getValue() * foodInfoBean.getReliang()) + " " + "千卡");
        mNameTx.setText(foodInfoBean.getName()+"");
        mCalories.setText(calories+" 千卡 / 100克 ");
        mTotalCalories = (int)(tapeView.getValue() * foodInfoBean.getReliang());

        tapeView.setOnValueChangeListener(value -> {
            flag = 2;
            mWeight.setText(value + " " + "克");
            mValue.setText(value + " " + "克");
            mCalories_d.setText((int)(value * foodInfoBean.getReliang()) + " " + "千卡");
            mTotalCalories = (int)(value * foodInfoBean.getReliang());
            callData();
        });

        foodTotalCalories.setCalories(mTotalCalories);
        foodTotalCalories.setRes_id(foodInfoBean.getId());
        foodTotalCalories.setName(foodInfoBean.getName());
        foodTotalCalories.setState(foodInfoBean.getState());


        System.out.println("init>>>>" + mTotalCalories);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.close_img :
                dismiss();
                break;
            case R.id.sure_btn:
                switch (state) {
                    case BREAKFAST_STATUS:
                        RxBus.getInstance().post(new BreakFastEvent<Object>(foodTotalCalories));
                        break;
                    case LUNCH_STATUS:
                        RxBus.getInstance().post(new LunchEvent<Object>(foodTotalCalories));
                        break;
                    case DINNER_STATUS:
                        RxBus.getInstance().post(new DinnerEvent<Object>(foodTotalCalories));
                        break;
                    default:
                }
                dismiss();
                break;
            default:
        }
    }

    private void callData(){
        if (flag == 2){
            Message message = new Message();
            Bundle bundle = new Bundle();
            bundle.putInt("gg",mTotalCalories);
            message.what = 100;
            message.setData(bundle);
            handler.sendMessage(message);
        }
    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            if (msg.what == 100) {
                foodTotalCalories.setCalories(mTotalCalories);
            }
        }
    };

}
